﻿using DeviceManager.DB;
using DeviceManager.DB.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DeviceManager.ContentPage.SysMng
{
    /// <summary>
    /// EditUserInfo.xaml 的交互逻辑
    /// </summary>
    public partial class EditUserInfo : Window
    {
        bool IsResetPassWord = false;
        private User nowEditUser = null;

        public EditUserInfo()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 设置编辑用户信息
        /// </summary>
        /// <param name="user"></param>
        public void SetEditUserInfo(User user)
        {
            IsResetPassWord = false;
            tb_userPwd.IsEnabled = false;
            tb_userPwd1.IsEnabled = false;
            tb_userLoginName.IsReadOnly = true;
            nowEditUser = user;
            UpDateInfo(user);
        }

        /// <summary>
        /// 重置用户密码
        /// </summary>
        /// <param name="user"></param>
        public void SetResetPassword(User user)
        {
            IsResetPassWord = true;
            tb_usernickName.IsReadOnly = true;
            tb_userLoginName.IsReadOnly = true;
            nowEditUser = user;
            UpDateInfo(user);
        }


        private void UpDateInfo(User user)
        {
            tb_userLoginName.Text = user.loginName;
            tb_usernickName.Text = user.nickName;
            tb_userPwd.Password = user.lginPwd;
            tb_userPwd1.Password = user.lginPwd;
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                this.DragMove();
            }
            catch { }
        }

        private void blt_OK_Click(object sender, RoutedEventArgs e)
        {
            if(tb_userLoginName.Text == "")
            {
                MessageBox.Show("新建用户必须输入登录名.");
                return;
            }
            if(tb_usernickName.Text == "")
            {
                MessageBox.Show("新建用户必须输入用户姓名");
                return;
            }
            if (!tb_userPwd.Password.Equals(tb_userPwd1.Password))
            {
                MessageBox.Show("两次密码不一致");
                return;
            }
            User user = new User();
            user.depID = 0;
            user.id = nowEditUser == null ? 0 : nowEditUser.id;
            user.lginPwd = tb_userPwd.Password;
            user.loginName = tb_userLoginName.Text;
            user.nickName = tb_usernickName.Text;
            if (ManagerDBCtrl.GetMng().SaveUserInfo(user))
            {
                nowEditUser = user;
                this.DialogResult = true;
            }

        }

        private void blt_Cannel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}
